// 深度 广度 优先搜索 统计树的最子节点
export const getTreeLeaf = (tree) => {
    let clone = JSON.parse(JSON.stringify(tree))
    const result = []
    while(clone.length){
        const node = clone.shift()
        if(node.children && node.children.length){
            clone = [...clone,...node.children]
        }else{
            result.push(node)
        }
    }
    return result
}


// 递归构建选中的树结构（保留选中节点及其路径）
export const buildSelectedTree = (sourceTree,checkedKeys) => {
    const clone = JSON.parse(JSON.stringify(sourceTree))
    return clone.filter(node => {
        const isChecked = checkedKeys.includes(node.id)
        if(isChecked){
            return true
        }
        if(node.children && node.children.length > 0){
            node.children = buildSelectedTree(node.children,checkedKeys)
            return node.children.length > 0
        }
        return false
    })
}